<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      border: 0;
      box-sizing: border-box;
      outline: none;
    }

    button {
      padding: 5px 10px;
      height: 35px;
      color: #fff;
      background-color: rgb(212, 12, 12);
    }

    button:active {
      background-color: rgb(179, 25, 25);
    }

    .flex-end {
      display: flex;
      justify-content: flex-end;
      align-items: center;
    }

    h3 {
      text-align: center;
      height: 60px;
      line-height: 60px;
      background-color: rgb(212, 12, 12);
      color: #fff;
    }

    ul {
      list-style: none;
    }

    li {
      padding: 10px 15px;
      border-bottom: 1px solid #ccc;
    }

    .bottom-box {
      padding: 15px 20px;
      position: fixed;
      width: 100%;
      bottom: 0;
    }
  </style>
</head>

<body>
  <h3>商品详情</h3>
  <ul id="list">
    <li>
      <span id="goodsName">商品</span>
      <span id="goodsPrice">1823</span>
    </li>
  </ul>
  <div class="bottom-box flex-end">
    <button id="addCart">加入购物车</button>
  </div>
  <script src="http://localhost:7001/package/axios.min.js"></script>
  <script src="http://localhost:7001/js/axios.js"></script><!-- 拦截器 -->

  <script>
    ; (async function () {
      const id = location.pathname.replace('/detail/', '')
      const res = await axios({
        method: "get",
        url: `/api/goodsDetail/${id}`
      })

      console.log(res);
      if (res.code < 400) {
        goodsName.innerHTML = res.data.name
        goodsPrice.innerHTML = res.data.price
      }


      addCart.onclick = async function () {
        const res = await axios({
          method: 'post',
          url: `/api/addCart`,
          data: {
            gid: id
          }
        })

        if (res.code < 400) {
          alert(res.msg)
        }
      }
    })()
  </script>
</body>

</html>